﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMSSyncData.Model.WMSModel;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.Oracle
{
    public class WMS_M_InStockRMDetails : BaseOracleDAL<WMS_T_InstockRMRowDetails>
    {
        public List<WMS_T_InstockRMRowDetails> GetSyncData()
        {
            var sql = @"SELECT  rawtohex(sys_guid()) as GUID,BillNo,RowNO,PartNumber,Version,Datecode,LotNo,BoxNo,RealQty AS PutawayQty,RealQty,'0' as status,RELATEDBILLNO as RelatedBill,
DATA_WRITETIME as CREATETIME,'admin' as Creator
FROM wms_M_InStockRMDetails  WHERE Data_Status=1";

            var dt = OracleDBHelper.GetDataSet(sql).Tables[0];
            var result = MakeTablePackage(dt);
            return result;
        }

        public int UpdateStatus(List<WMS_T_InstockRMRowDetails> list)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("begin");
            foreach (var item in list)
            {
                var sql = "UPDATE WMS_M_INSTOCKDETAILS set  Data_Status=2,Data_ReadTime=SYSDATE where BillNo='" + item.BillNo + "' and rowNo='" + item.RowNo + "';";
                sb.AppendLine(sql);
            }
            sb.AppendLine("end;");

            var resultSql = sb.ToString().Trim();
            var result = OracleDBHelper.ExecuteNonQuery(resultSql);
            return result;
        }


        public DataTable getUpdateDate()
        {
            var sql = @" select NVL(PLANQTY,0) AS PLANQTY,UniqueCode from wms_M_InStockRMDetails  WHERE Data_Status=1	";

            var dt = OracleDBHelper.GetDataSet(sql).Tables[0];
             
            return dt;
        }

        public int UpdateMInStockStatus(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("begin");
            foreach (DataRow dr in dt.Rows)
            {
                var sql = "UPDATE wms_M_InStockRMDetails set  Data_Status='2' where UniqueCode='" + dr["UniqueCode"].ToString() + "';";
                sb.AppendLine(sql);
            }
            sb.AppendLine("end;");

            var resultSql = sb.ToString().Trim();
            var result = OracleDBHelper.ExecuteNonQuery(resultSql);
            return result;
        }

    }
}
